package cn.itcast.controller.cargo;

import cn.itcast.base.BaseController;
import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.Invoice;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.service.ExportService;
import cn.itcast.service.InvoiceService;
import cn.itcast.service.PackingService;
import cn.itcast.utils.DownloadUtil;
import cn.itcast.utils.FileUploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

@Controller
@RequestMapping(value = "/cargo/invoice")
public class InvoiceController extends BaseController {
    @Reference
    private InvoiceService invoiceService;
    @Autowired
    private FileUploadUtil fileUploadUtil;
    @Reference
    private PackingService packingService;
    @Reference
    private ExportService exportService;
//    @Reference
//    private ShippingService shippingService;


    @RequestMapping(value = "/list.do")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int PageSize) {
        PageInfo pageInfo = invoiceService.findPage(pageNum, PageSize);
        request.setAttribute("pb", pageInfo);
        return "cargo/invoice/invoice-list";
    }

    @RequestMapping(value = "/delete.do")
    public String delete(String id) {
        Invoice invoice = invoiceService.selectById(id);
        Integer state = invoice.getState();
        if (state == 0) {
            invoiceService.deleteByid(id);
        }
        return "redirect:/cargo/invoice/list.do";
    }

    //, MultipartFile pickupPhoto
    //String invoiceId,String scNo,String tradeTerms,int invoiceMoney
    @RequestMapping(value = "/edit")
    public String edit(Invoice invoice, MultipartFile fileName) throws Exception {

        // 保存图片到七牛云
       String imgSrc = fileUploadUtil.upload(fileName);

        //Invoice invoice = new Invoice();
        //invoice.setInvoiceId(invoiceId);
        //int packingMoney1 = Integer.parseInt(invoiceMoney);

        //.setInvoiceMoney(invoiceMoney);
       invoice.setPickupPhoto("http://" + imgSrc);

        //invoice.setTradeTerms(tradeTerms);
        //invoice.setScNo(scNo);//报云合同号
        //invoice.setInvoiceTime(new Date().toString());
        invoice.setState(0);
        invoiceService.addInvoice(invoice);
        //shippingService.
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "/toAdd.do")
    public String toAdd(String id) throws Exception {
        Packing packing = packingService.findById(id);
        String exportNos = packing.getExportNos();//报云合同号
        String exportIds = packing.getExportIds();
        Invoice invoice = new Invoice();
        if (exportIds != null) {
            String[] exportIdss = exportIds.split(",");
            StringBuffer trideTerms = new StringBuffer();
            String priceCondition = null;
            for (String s : exportIdss) {
                Export export = exportService.findById(s);
                priceCondition = export.getPriceCondition();
                trideTerms.append(priceCondition).append(",");
            }
            //request.setAttribute("tradeTerms",trideTerms);
            invoice.setTradeTerms(trideTerms.toString());
        }


        Double packingMoney = packing.getPackingMoney();
//        request.setAttribute("scNo",exportNos);
//        request.setAttribute("invoiceMoney",packingMoney);
//        request.setAttribute("invoiceId",id);
        invoice.setScNo(exportNos);
        Double reseve3 = Double.parseDouble(packingMoney.toString());
        int b = reseve3.intValue();
        Integer rentCount = Integer.valueOf(b);
        invoice.setInvoiceMoney(rentCount);
        invoice.setInvoiceId(id);

        request.setAttribute("invoice", invoice);

        return "cargo/invoice/invoice-add";
    }

    @RequestMapping(value = "/checked.do")
    public String checked(String id) {
        Invoice invoice = invoiceService.selectById(id);
        invoice.setState(1);
        invoiceService.updateInvoice(invoice);
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "/invoiceE.do")
    public void invoiceE(String id) throws IOException {
        Invoice invoice = invoiceService.selectById(id);

        //2 将数据写入到excel表中
        // 1 创建工作薄和工作页以及设置列宽
        Workbook wb = new SXSSFWorkbook();
        Sheet st = wb.createSheet();
        st.setColumnWidth(1, 20 * 256);
        st.setColumnWidth(2, 20 * 256);
        st.setColumnWidth(3, 20 * 256);
        st.setColumnWidth(4, 20 * 256);
        st.setColumnWidth(5, 20 * 256);
        st.setColumnWidth(6, 20 * 256);


        int rowIndex = 0; //行数
        Row row;  //行
        Cell cell; //单元

        // 2 设置第一行
        row = st.createRow(rowIndex++);//初始0  用完就+1
        row.setHeightInPoints(36); //行高
        cell = row.createCell(1); //创建单元格
        st.addMergedRegion(new CellRangeAddress(0, 0, 1, 6)); //合并单元

        cell.setCellValue("黑马商务发展有限公司发票"); //设置单元格数据
        CellStyle cellStyle = bigTitle(wb);//设置第一行数据的样式
        cell.setCellStyle(cellStyle);
        // 3 设置第二行
        row = st.createRow(rowIndex++);//初始1  用完就+1
        row.setHeightInPoints(26); //行高
        String[] arr = {"", "发票编号", "报运合同号", "贸易条款", "发票金额", "发票时间", "发票状态"};
        for (int i = 1; i < arr.length; i++) {
            //创建第二行的单元格
            cell = row.createCell(i);
            //给创建的该单元格放入数据
            cell.setCellValue(arr[i]);
            //给单元格设置样式
            cell.setCellStyle(title(wb));
        }

        row = st.createRow(rowIndex++);
        //设置行高
        row.setHeightInPoints(25);
        //创建单元格
        cell = row.createCell(1);
        //给单元格内容
        cell.setCellValue(invoice.getInvoiceId());
        //给单元格样式
        cell.setCellStyle(text(wb));

        //创建单元格
        cell = row.createCell(2);
        //给单元格内容
        cell.setCellValue(invoice.getScNo());
        //给单元格样式
        cell.setCellStyle(text(wb));

        //创建单元格
        cell = row.createCell(3);
        //给单元格内容
        cell.setCellValue(invoice.getTradeTerms());
        //给单元格样式
        cell.setCellStyle(text(wb));

        //创建单元格
        cell = row.createCell(4);
        //给单元格内容
        cell.setCellValue(invoice.getInvoiceMoney());
        //给单元格样式
        cell.setCellStyle(text(wb));

        //创建单元格
        cell = row.createCell(5);
        //给单元格内容
        cell.setCellValue(invoice.getInvoiceTime());
        //给单元格样式
        cell.setCellStyle(text(wb));

        //创建单元格
        cell = row.createCell(6);
        //给单元格内容
        String ss = null;
        if (invoice.getState() == 0){
            ss = "未支付";
        }else {
            ss = "已支付";
        }
        cell.setCellValue(ss);
        //给单元格样式
        cell.setCellStyle(text(wb));


        //3 下载准备好的出货表
        //文件名
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        DownloadUtil downloadUtil = new DownloadUtil();
        wb.write(byteArrayOutputStream);
        downloadUtil.download(byteArrayOutputStream, response, "发票.xlsx");
    }


    //大标题的样式
    public CellStyle bigTitle(Workbook wb) {
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 16);
        font.setBold(true);//字体加粗
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);                //横向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);        //纵向居中
        return style;
    }

    //小标题的样式
    public CellStyle title(Workbook wb) {
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("黑体");
        font.setFontHeightInPoints((short) 12);
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);                //横向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);        //纵向居中
        style.setBorderTop(BorderStyle.THIN);                        //上细线
        style.setBorderBottom(BorderStyle.THIN);                    //下细线
        style.setBorderLeft(BorderStyle.THIN);                        //左细线
        style.setBorderRight(BorderStyle.THIN);                        //右细线
        return style;
    }

    //文字样式
    public CellStyle text(Workbook wb) {
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("Times New Roman");
        font.setFontHeightInPoints((short) 10);

        style.setFont(font);

        style.setAlignment(HorizontalAlignment.LEFT);                //横向居左
        style.setVerticalAlignment(VerticalAlignment.CENTER);        //纵向居中
        style.setBorderTop(BorderStyle.THIN);                        //上细线
        style.setBorderBottom(BorderStyle.THIN);                    //下细线
        style.setBorderLeft(BorderStyle.THIN);                        //左细线
        style.setBorderRight(BorderStyle.THIN);                        //右细线

        return style;
    }

}
